#include "actionfilewriter.h"

ActionFileWriter::ActionFileWriter()
{
}


void ActionFileWriter::write(QString filename, RobotAction *action){
    QTextStream outStream;

    //Open up a file and stream to write
    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    outStream.setDevice(&file);

    outStream << action;
}

void ActionFileWriter::writeLegacyFile(QString filename, RobotAction *action){
    int i,j;
    QTextStream outStream;
    QList<int> jointIDs;
    QList<MotionKeyFrame> keyFrames;
    MotionKeyFrame curKeyFrame;
    QPair<int, float> curJointValue;

    //Open up a file and stream to write
    QFile file(filename);
    if(!file.open(QIODevice::WriteOnly | QIODevice::Text))
        return;
    outStream.setDevice(&file);

    //Write the header
    outStream << action->name() << "\n";
    outStream << "timestamp";
    outStream.setFieldWidth(8);
    jointIDs = action->activeJointIDs();
    for(i = 0; i < jointIDs.length(); i++){
        outStream << jointIDs.at(i);
    }
    outStream << "\n";

    //Write the joint positions
    keyFrames = action->keyFrames();

    for(i = 0; i < keyFrames.length(); i++){
        curKeyFrame = keyFrames[i];
        //Timestamp
        outStream << curKeyFrame.timestamp;

        for(j = 0; j < curKeyFrame.jointValues.count(); j++){
            curJointValue = curKeyFrame.jointValues[j];

            //A big assumption is being made that each
            //joint value in the keyframe corresponds
            //precisely with the joint ID
            //Q_ASSERT(curJointValue.first == jointIDs[j]);

            outStream << curJointValue.second;
        }

        outStream << "\n";
    }
}

QTextStream &operator<<(QTextStream &out, const RobotAction &action){
    return out;







    return out;
}

